package com.network.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.widget.ImageView;

import java.io.InputStream;


/**
 * Created by yangweirong on 15/1/9.
 */
public class NetworkUtils {
    private final static String WIFI = "wifi";
    private final static String CMNET = "cmnet";
    private final static String CMWAP = "cmwap";
    private final static String _3GWAP = "3gwap";
    private final static String _3GNET = "3gnet";
    private final static String UNIWAP = "uniwap";
    private final static String UNINET = "uninet";
    public static Context appContext;

    private static SharedPreferences sharedPreferences; //add shibo.zheng

    public static void init(Context ctx) {
        appContext = ctx;
        sharedPreferences = appContext.getSharedPreferences(appContext.getPackageName(), Context.MODE_PRIVATE); //add shibo.zheng
    }

    /**
     * 检测网络是否可用
     *
     * @return
     */
    public static boolean isNetworkConnected() {
        if (appContext == null) {
            throw new NullPointerException("请在自定义Application中调用init方法");
        }

        ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        }
        NetworkInfo ni = cm.getActiveNetworkInfo();
        boolean result = ni != null && ni.isConnectedOrConnecting();
        //
        return result;
    }

    public static int getLocalIpAddress() {
        if (appContext == null) {
            throw new NullPointerException("请在自定义Application中调用init方法");
        }
        WifiManager wifiManager = (WifiManager) appContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        return ipAddress;
    }

    public static String getNetworkType() {
        if (appContext == null) {
            throw new NullPointerException("请在自定义Application中调用init方法");
        }
        ConnectivityManager manager = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        String netTypeStr = "";
        // 通过ConnectivityManager得到NetworkInfo网络信息
        NetworkInfo info = manager.getActiveNetworkInfo();
        // 获取NetworkInfo中的apn信息
        if (info != null) {
            int netType = info.getType(); // WIFI/MOBILE
            if (netType == ConnectivityManager.TYPE_WIFI) {
                netTypeStr = WIFI;
            } else if (netType == ConnectivityManager.TYPE_MOBILE) {
                if (!TextUtils.isEmpty(info.getExtraInfo())) {
                    String netName = info.getExtraInfo().toLowerCase();
                    /**
                     * 用移动CMNET方式 getExtraInfo 的值是cmnet
                     * 用移动CMWAP方式 getExtraInfo 的值是cmwap 但是不在代理的情况下访问普通的网站访问不了
                     * 用联通3gwap方式 getExtraInfo 的值是3gwap
                     * 用联通3gnet方式 getExtraInfo 的值是3gnet
                     * 用联通uniwap方式 getExtraInfo 的值是uniwap
                     * 用联通uninet方式 getExtraInfo 的值是uninet
                     */
                    if (netName.equals(CMNET)) {
                        netTypeStr = CMNET;
                    } else if (netName.equals(CMWAP)) {
                        netTypeStr = CMWAP;
                    } else if (netName.equals(_3GNET)) {
                        netTypeStr = _3GNET;
                    } else if (netName.equals(_3GWAP)) {
                        netTypeStr = _3GWAP;
                    } else if (netName.equals(UNINET)) {
                        netTypeStr = UNINET;
                    } else if (netName.equals(UNIWAP)) {
                        netTypeStr = UNIWAP;
                    }
                }
            }
        }
        return netTypeStr;
    }

    public static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        @Override
        protected Bitmap doInBackground(String... urls) {
            String urlDisplay = urls[0];
            Bitmap mIcon11;
            try {
                InputStream in = new java.net.URL(urlDisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                return null;
            }
            return mIcon11;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

    /**
     * add shibo.zheng 19/07/03
     * 获取token
     */

    public static String getToken() {
        return sharedPreferences.getString("token", "");
    }

    /**
     * 存储token
     */
    public static void saveToken(String token) {
        sharedPreferences.edit().putString("token", token).apply();
    }

    /**
     * 删除token
     */

    public static void deleteToken() {
        sharedPreferences.edit().putString("token", "").apply();
    }

}
